package com.ysstech.system.controller;

import java.time.LocalDateTime;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Region;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.system.service.RegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 字典信息前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-01-11
 */
@Controller
@Slf4j
@Api(tags = "按钮信息管理api")
@RequestMapping("/region")

public class RegionController extends BaseController {
    @Autowired
    private RegionService regionService;


    @YssPermissions(value = {"system:menu:getRegion"})
    @ApiOperation(value = "获取按钮信息")
    @YssLog(module = "按钮管理", remark = "获取按钮信息")
    @PostMapping(value = "/getRegion")
    @ResponseBody
    public RestResult getRegion(String menuId) throws Exception {
        if (StringUtils.isBlank(menuId)) {
            throw new BusinessException("获取按钮对应的信息失败，请稍后重试！");
        }
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("MENU_ID", menuId);
        List<Region> list = regionService.list(wrapper);
        return ResultGenerator.getSuccessResult("查询按钮详细值信息成功", list);
    }

    /**
     * 添加按钮
     *
     * @param region
     * @return
     */
    @YssPermissions(value = {"system:menu:addRegion"})
    @ApiOperation(value = "添加按钮")
    @YssLog(module = "按钮管理", remark = "添加按钮")
    @PostMapping(value = "/addRegion")
    @ResponseBody
    public RestResult addRegion(Region region) throws Exception {
        region.setId(DateUtil.generateUUID());
        region.setAddTime(LocalDateTime.now());
        regionService.save(region);
        return ResultGenerator.getSuccessResult("新增按钮成功！");
    }

    /**
     * 修改字典对应值
     *
     * @param region
     * @return
     */
    @YssPermissions(value = {"system:menu:editRegion"})
    @ApiOperation(value = "修改按钮")
    @YssLog(module = "按钮管理", remark = "修改按钮")
    @PostMapping(value = "/editRegion")
    @ResponseBody
    public RestResult editRegion(Region region) throws Exception {
        regionService.updateById(region);
        return ResultGenerator.getSuccessResult("修改按钮成功！");
    }

    /**
     * 删除按钮
     *
     * @param ids
     * @return
     */
    @YssPermissions(value = {"system:menu:delRegion"})
    @ApiOperation(value = "删除按钮")
    @YssLog(module = "按钮管理", remark = "删除按钮")
    @PostMapping(value = "/delRegion")
    @ResponseBody
    public RestResult delRegion(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            return ResultGenerator.getBusinessResult("删除按钮失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        regionService.removeByIds(list);
        return ResultGenerator.getSuccessResult("删除按钮成功！");
    }
}
